/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.gui;

import L.L;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

/**
 *
 * @author Tobias
 */
public class DnDTransferHandler extends TransferHandler {

    /**
     * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent,
     *      java.awt.datatransfer.DataFlavor[])
     */
    public boolean canImport(JComponent arg0, DataFlavor[] arg1) {
        for (int i = 0; i < arg1.length; i++) {
            DataFlavor flavor = arg1[i];
            if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                L.d("canImport: JavaFileList FLAVOR: " + flavor);
                return true;
            }
            if (flavor.equals(DataFlavor.stringFlavor)) {
                L.d("canImport: String FLAVOR: " + flavor);
                return true;
            }
            System.err.println("canImport: Rejected Flavor: " + flavor);
        }
        // Didn't find any that match, so:
        return false;
    }

    /**
     * Do the actual import.
     * 
     * @see javax.swing.TransferHandler#importData(javax.swing.JComponent,
     *      java.awt.datatransfer.Transferable)
     */
    public boolean importData(JComponent comp, Transferable t) {
        DataFlavor[] flavors = t.getTransferDataFlavors();
        L.d("Trying to import:" + t);
        L.d("... which has " + flavors.length + " flavors.");
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            try {
                if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                    L.d("importData: FileListFlavor");

                    List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
                    Iterator iter = l.iterator();
                    while (iter.hasNext()) {
                        File file = (File) iter.next();
                        L.d("GOT FILE: " + file.getCanonicalPath());
                    // Now do something with the file...
                    }
                    return true;
                } else if (flavor.equals(DataFlavor.stringFlavor)) {
                    L.d("importData: String Flavor");
                    String fileOrURL = (String) t.getTransferData(flavor);
                    L.d("GOT STRING: " + fileOrURL);
                    try {
                        URL url = new URL(fileOrURL);
                        L.d("Valid URL: " + url.toString());
                        // Do something with the contents...
                        return true;
                    } catch (MalformedURLException ex) {
                        System.err.println("Not a valid URL");
                        return false;
                    }
                // now do something with the String.

                } else {
                    L.d("importData rejected: " + flavor);
                // Don't return; try next flavor.
                }
            } catch (IOException ex) {
                System.err.println("IOError getting data: " + ex);
            } catch (UnsupportedFlavorException e) {
                System.err.println("Unsupported Flavor: " + e);
            }
        }
        // If you get here, I didn't like the flavor.
        Toolkit.getDefaultToolkit().beep();
        return false;
    }
}
